<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台管理</title>
    <link rel="icon" href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
    <style>

    </style>
</head>
<body>
<div class="layui-fluid">
    <hr class="layui-bg-red">
    <div class="layui-row">
        <div class="layui-col-sm10  layui-col-sm-offset1" >
            <form class="layui-form layui-form-pane" lay-filter="MenuEdit" id="menuForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" autofocus lay-verify="required|name" lay-verType="tips" placeholder="请输入菜单名" class="layui-input" id="name">
                    </div>
                    </div>
				<div class="layui-form-item">
                    <label class="layui-form-label">菜单类型</label>
                    <div class="layui-input-block">
                        <input type="text" name="type" autofocus lay-verify="required|type" lay-verType="tips" placeholder="请输入菜单类型（0：菜单，1：按钮）" class="layui-input" id="type">
                    </div>
                </div>
	
				<div class="layui-form-item">
					<input type="hidden" name="parentId">
					<label class="layui-form-label">上级菜单</label>
					<div class="layui-input-block">
						<select id="parentId" name="parentId" required lay-verType="tips" lay-verify="department" lay-filter="selectDept" lay-search>
							<option value="1">顶级菜单</option>
						</select>
					</div>
				</div>
	<%--
                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单</label>
                    <div class="layui-input-block">
                        <input type="text" name="parentId" lay-verify="required|parentId" lay-verType="tips" placeholder="请输入上级菜单" class="layui-input" id="parentId">
                        <input type="hidden" name="deptId" value="1">
                    </div>
                </div>--%>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单权限</label>
                    <div class="layui-input-block">
                        <input type="text" name="permission" lay-verify="required" lay-verType="tips" placeholder="请输入当前菜单所属权限" class="layui-input" id="permission">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单url</label>
                    <div class="layui-input-block">
                        <input type="text" name="url"  lay-verType="tips" placeholder="请输入当前菜单的url" class="layui-input" id="url">
                    </div>
                </div>
				<div class="layui-form-item">
					<label class="layui-form-label">排序</label>
					<div class="layui-input-block">
						<input type="text" name="sort"  lay-verType="tips" lay-verify="required|sort" placeholder="请输入该菜单顺序号" class="layui-input" id="sort">
					</div>
				</div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input type="text" name="remark" lay-verType="tips" placeholder="请输入备注" class="layui-input" id="note">
                    </div>
                </div>
                
                <div class="layui-form-item" style="display: none">
                    <label class="layui-form-label">菜单ID</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id"  placeholder="此ID不允许改动" autocomplete="off" class="layui-input" id="id">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="saveMenu">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var baseUrl = '${pageContext.request.contextPath}/sys/menu/';

    /*发送ajax请求访问后台*/
    function sendAjaxPostReq($, param, method) {
        $.post(
            baseUrl + method,
            param,
            function (result) {
                if (result.code == 0) {
                    layer.msg(result.msg, {icon: 1, time: 500}, function () {
                        parent.window.location.reload();
                    });
                } else {
                    layer.msg(result.msg, {icon: 5, anim: 6});
                }
            },
            "json"
        );
    }

    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.$;
	
	
	/*加载部门信息*/
	function showDeptSelectInfo() {
		var parentId = $("body").find("input[name='parentId']").val();
		$.post(
		baseUrl + "getAll",
		function (result) {
		var parentIds = result.data;
		$.each(parentIds, function (index, parentIds) {
		/*修改时选中状态*/
		if (parentIds.id == parentId) {
			$("#parentId").append("<option selected value='" + parentIds.id + "'>" + parentIds.name + "</option>");
			} else {
				$("#parentId").append("<option value='" + parentIds.id + "'>" + parentIds.name + "</option>");
				}
		});
		form.render('select');
			return false;
		},
		"json"
		);
	}
	$(function () {
		/*加载部门信息*/
		showDeptSelectInfo();
		/*validateUsernameOnly();*/
	});
        //自定义验证规则
        form.verify({
                name: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '用户名不能全为数字';
                }
            },
			sort: [/^\d{0,5}$/, '上级菜单必须为5位以内数字代码'],
			type: [/^[0-1]?$/, '上级菜单必须为0（菜单）或1（按钮）'],
            password: [/(.+){6,12}$/, '密码必须6到12位']
        });
        /*表单提交*/
        var id=document.getElementById("id").value;
        var edit="add";
        if(id){edit="edit";}
        form.on('submit(saveMenu)', function (data) {
            sendAjaxPostReq($, data.field, edit);
            return false;
        });
    });
</script>
</body>
</html>
